
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>字符串 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="date-time.html" />
    
    
    <link rel="prev" href="comprehensions.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="basics.html">
            
                <a href="basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="collections.html">
            
                <a href="collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="enum.html">
            
                <a href="enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="pattern-matching.html">
            
                <a href="pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="control-structures.html">
            
                <a href="control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="functions.html">
            
                <a href="functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="pipe-operator.html">
            
                <a href="pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="modules.html">
            
                <a href="modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="mix.html">
            
                <a href="mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="sigils.html">
            
                <a href="sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="documentation.html">
            
                <a href="documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="testing.html">
            
                <a href="testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="comprehensions.html">
            
                <a href="comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.15" data-path="strings.html">
            
                <a href="strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="date-time.html">
            
                <a href="date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="mix-tasks.html">
            
                <a href="mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="iex-helpers.html">
            
                <a href="iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../advanced/erlang.html">
            
                <a href="../advanced/erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../advanced/error-handling.html">
            
                <a href="../advanced/error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../advanced/escripts.html">
            
                <a href="../advanced/escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../advanced/concurrency.html">
            
                <a href="../advanced/concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../advanced/otp-concurrency.html">
            
                <a href="../advanced/otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../advanced/otp-supervisors.html">
            
                <a href="../advanced/otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../advanced/otp-distribution.html">
            
                <a href="../advanced/otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../advanced/metaprogramming.html">
            
                <a href="../advanced/metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../advanced/umbrella-projects.html">
            
                <a href="../advanced/umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../advanced/typespec.html">
            
                <a href="../advanced/typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="../advanced/behaviours.html">
            
                <a href="../advanced/behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="../advanced/gen-stage.html">
            
                <a href="../advanced/gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="../advanced/protocols.html">
            
                <a href="../advanced/protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="../advanced/nerves.html">
            
                <a href="../advanced/nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >字符串</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x5B57;&#x7B26;&#x5217;&#x8868;&#x3001;&#x5B57;&#x7D20;(Graphemes) &#x548C;&#x5B57;&#x7801;&#x70B9;&#xFF08;Codepoints&#xFF09;&#x3002;</p>
<h2 id="&#x5B57;&#x7B26;&#x4E32;">&#x5B57;&#x7B26;&#x4E32;</h2>
<p>Elixir &#x5B57;&#x7B26;&#x4E32;&#x5C31;&#x662F;&#x5B57;&#x8282;&#x5E8F;&#x5217;&#xFF0C;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; string = &lt;&lt;<span class="hljs-number">104</span>,<span class="hljs-number">101</span>,<span class="hljs-number">108</span>,<span class="hljs-number">108</span>,<span class="hljs-number">111</span>&gt;&gt;
<span class="hljs-string">&quot;hello&quot;</span>
iex&gt; string &lt;&gt; &lt;&lt;<span class="hljs-number">0</span>&gt;&gt;
&lt;&lt;<span class="hljs-number">104</span>, <span class="hljs-number">101</span>, <span class="hljs-number">108</span>, <span class="hljs-number">108</span>, <span class="hljs-number">111</span>, <span class="hljs-number">0</span>&gt;&gt;
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;&#x5F53;&#x5728; <code>string</code> &#x540E;&#x9762;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x5B57;&#x8282; <code>0</code> &#x4E4B;&#x540E;&#xFF0C;IEx &#x5C06; <code>string</code> &#x663E;&#x793A;&#x4E3A;&#x4E8C;&#x8FDB;&#x5236;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x5DF2;&#x7ECF;&#x4E0D;&#x518D;&#x662F;&#x4E00;&#x4E2A;&#x5408;&#x6CD5;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x4E86;&#x3002;&#x8FD9;&#x4E2A;&#x5C0F;&#x6280;&#x5DE7;&#x53EF;&#x4EE5;&#x8BA9;&#x6211;&#x4EEC;&#x770B;&#x5230;&#x6240;&#x6709;&#x5B57;&#x7B26;&#x4E32;&#x5E95;&#x5C42;&#x7684;&#x5B57;&#x8282;&#x3002;</p>
<blockquote>
<p>NOTE: &#x4F7F;&#x7528; <code>&lt;&lt; &gt;&gt;</code> &#x662F;&#x544A;&#x8BC9;&#x7F16;&#x8BD1;&#x5668;&#x8FD9;&#x4E2A;&#x7B26;&#x53F7;&#x91CC;&#x9762;&#x7684;&#x5185;&#x5BB9;&#x662F;&#x5B57;&#x8282;&#x3002;</p>
</blockquote>
<h2 id="&#x5B57;&#x7B26;&#x5217;&#x8868;">&#x5B57;&#x7B26;&#x5217;&#x8868;</h2>
<p>&#x5728; Elixir &#x5185;&#x90E8;&#xFF0C;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x5B57;&#x8282;&#x5E8F;&#x5217;&#x8868;&#x793A;&#x7684;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x5B57;&#x7B26;&#x6570;&#x7EC4;&#x3002;Elixir &#x4E5F;&#x6709;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x5217;&#x8868;&#x7684;&#x7C7B;&#x578B;&#xFF1A;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x53CC;&#x5F15;&#x53F7;&#x62EC;&#x8D77;&#x6765;&#x7684;&#xFF0C;&#x800C;&#x5B57;&#x7B26;&#x5217;&#x8868;&#x662F;&#x5355;&#x5F15;&#x53F7;&#x62EC;&#x8D77;&#x6765;&#x7684;&#x3002;</p>
<p>&#x8FD9;&#x4E24;&#x8005;&#x6709;&#x4EC0;&#x4E48;&#x533A;&#x522B;&#x5462;&#xFF1F;&#x5B57;&#x7B26;&#x5217;&#x8868;&#x7684;&#x6BCF;&#x4E2A;&#x503C;&#x90FD;&#x662F;&#x5B57;&#x7B26;&#x7684; UTF-8 &#x7684;&#x7801;&#x70B9;&#xFF08;Codepoint&#xFF09;, &#x800C;&#x5B57;&#x7B26;&#x4E32;&#x91CC;&#x9762;&#x7684;&#x503C;&#x662F;&#x5B57;&#x7B26;&#x7684;&#x4E8C;&#x8FDB;&#x5236;&#x5B57;&#x8282;&#x3002;&#x6211;&#x4EEC;&#x6765;&#x6DF1;&#x5165;&#x4E86;&#x89E3;&#x4E00;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; <span class="hljs-string">&apos;he&#x142;&#x142;o&apos;</span>
[<span class="hljs-number">104</span>, <span class="hljs-number">101</span>, <span class="hljs-number">322</span>, <span class="hljs-number">322</span>, <span class="hljs-number">111</span>]
iex&gt; <span class="hljs-string">&quot;he&#x142;&#x142;o&quot;</span> &lt;&gt; &lt;&lt;<span class="hljs-number">0</span>&gt;&gt;
&lt;&lt;<span class="hljs-number">104</span>, <span class="hljs-number">101</span>, <span class="hljs-number">197</span>, <span class="hljs-number">130</span>, <span class="hljs-number">197</span>, <span class="hljs-number">130</span>, <span class="hljs-number">111</span>, <span class="hljs-number">0</span>&gt;&gt;
</code></pre>
<p><code>322</code> &#x662F; &#x142; &#x7684; UTF-8 &#x7801;&#x70B9;&#xFF0C;&#x4F46;&#x662F;&#x5B83;&#x5728; UTF-8 &#x4E2D;&#x88AB;&#x7F16;&#x7801;&#x4E3A;&#x4E24;&#x4E2A;&#x5B57;&#x8282;&#xFF1A; <code>197</code> &#x548C; <code>130</code>&#x3002;</p>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; <code>?</code> &#x6765;&#x83B7;&#x5F97;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x7684;&#x7801;&#x70B9;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; ?Z
<span class="hljs-number">90</span>
</code></pre>
<p>&#x901A;&#x8FC7; <code>?Z</code> &#x4F60;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x53D6;&#x5F97; Z &#x7684;&#x7801;&#x70B9;&#x800C;&#x4E0D;&#x662F; Z &#x8FD9;&#x4E2A;&#x5B57;&#x7B26;&#x3002;</p>
<p>&#x5728;&#x4F7F;&#x7528; Elixir &#x7F16;&#x7A0B;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x901A;&#x5E38;&#x4F1A;&#x4F7F;&#x7528;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x5B57;&#x7B26;&#x5217;&#x8868;&#x3002;&#x5B57;&#x7B26;&#x5217;&#x8868;&#x4E4B;&#x6240;&#x4EE5;&#x5B58;&#x5728;&#xFF0C;&#x662F;&#x56E0;&#x4E3A;&#x6709;&#x4E9B; Erlang &#x6A21;&#x5757;&#x8981;&#x7528;&#x5230;&#x5B83;&#x3002;</p>
<p>&#x66F4;&#x591A;&#x7684;&#x4FE1;&#x606F;&#x8BF7;&#x53C2;&#x8003;&#x5B98;&#x65B9;&#x6587;&#x6863; <a href="http://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html" target="_blank"><code>Getting Started Guide</code></a>.</p>
<h2 id="&#x5B57;&#x7D20;&#x548C;&#x5B57;&#x7801;&#x70B9;">&#x5B57;&#x7D20;&#x548C;&#x5B57;&#x7801;&#x70B9;</h2>
<p>&#x7801;&#x70B9;&#xFF08;Codepoint&#xFF09;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x6216;&#x8005;&#x591A;&#x4E2A;&#x5B57;&#x8282;&#x8868;&#x793A;&#x7684; Unicode &#x5B57;&#x7B26;&#xFF08;&#x6839;&#x636E; UTF-8 &#x7F16;&#x7801;&#x65B9;&#x5F0F;&#xFF0C;&#x6BCF;&#x4E2A;&#x5B57;&#x7801;&#x70B9;&#x4F1A;&#x6709;&#x957F;&#x5EA6;&#x4E0D;&#x540C;&#xFF09;&#x3002;ASCII &#x7801;&#x4E4B;&#x5916;&#x7684;&#x5B57;&#x7B26;&#x4E00;&#x822C;&#x90FD;&#x4F1A;&#x7531;&#x591A;&#x4E2A;&#x5B57;&#x8282;&#x8868;&#x793A;&#xFF0C;&#x6BD4;&#x5982;&#x5E26;&#x6709;&#x6CE2;&#x6D6A;&#x7EBF;&#x6216;&#x8005;&#x58F0;&#x8C03;&#x7684;&#x62C9;&#x4E01;&#x5B57;&#x7B26; (<code>&#xE1;, &#xF1;, &#xE8;</code>) &#x4E00;&#x822C;&#x90FD;&#x662F;&#x4E24;&#x4E2A;&#x5B57;&#x8282;&#x8868;&#x793A;&#x7684;&#x3002;&#x800C;&#x4E9A;&#x6D32;&#x8BED;&#x8A00;&#x7684;&#x5B57;&#x7B26;&#x4E00;&#x822C;&#x90FD;&#x662F;&#x4E09;&#x4E2A;&#x6216;&#x8005;&#x56DB;&#x4E2A;&#x5B57;&#x8282;&#x3002;&#x5B57;&#x7D20;&#xFF08;Graphemes&#xFF09;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x7684;&#x8868;&#x793A;&#xFF0C;&#x901A;&#x5E38;&#x7531;&#x591A;&#x4E2A;&#x5B57;&#x7801;&#x70B9;&#x7EC4;&#x6210;&#x3002;</p>
<p><code>String</code> &#x6A21;&#x5757;&#x5DF2;&#x7ECF;&#x63D0;&#x4F9B;&#x4E86;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x6765;&#x83B7;&#x53D6;&#x8FD9;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x7684;&#x503C;&#xFF1A;<code>graphemes/1</code> &#x548C; <code>codepoints/1</code>&#x3002;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x4E00;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; string = <span class="hljs-string">&quot;\u0061\u0301&quot;</span>
<span class="hljs-string">&quot;&#xE1;&quot;</span>

iex&gt; String.codepoints string
[<span class="hljs-string">&quot;a&quot;</span>, <span class="hljs-string">&quot;&#x301;&quot;</span>]

iex&gt; String.graphemes string
[<span class="hljs-string">&quot;&#xE1;&quot;</span>]
</code></pre>
<h2 id="&#x5B57;&#x7B26;&#x4E32;&#x51FD;&#x6570;">&#x5B57;&#x7B26;&#x4E32;&#x51FD;&#x6570;</h2>
<p>&#x8FD9;&#x4E2A;&#x90E8;&#x5206;&#x6211;&#x4EEC;&#x770B;&#x4E00;&#x4E0B; <code>String</code> &#x6A21;&#x5757;&#x6700;&#x5E38;&#x7528;&#x7684;&#x4E00;&#x4E9B;&#x51FD;&#x6570;&#x3002;&#x672C;&#x8BFE;&#x7A0B;&#x53EA;&#x4F1A;&#x4ECB;&#x7ECD;&#x5176;&#x4E2D;&#x7684;&#x4E00;&#x90E8;&#x5206;&#xFF0C;&#x5982;&#x679C;&#x8981;&#x4E86;&#x89E3;&#x6240;&#x6709;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x8BF7;&#x53C2;&#x8003;<a href="https://hexdocs.pm/elixir/String.html" target="_blank">&#x5B98;&#x65B9; <code>String</code> &#x6587;&#x6863;</a>&#x3002;</p>
<h3 id="length1"><code>length/1</code></h3>
<p>&#x8FD4;&#x56DE;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x5B57;&#x7D20;&#x7684;&#x6570;&#x91CF;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; String.length(<span class="hljs-string">&quot;Hello&quot;</span>)
<span class="hljs-number">5</span>
</code></pre>
<h3 id="replace3"><code>replace/3</code></h3>
<p>&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5B83;&#x7684;&#x503C;&#x662F;&#x628A;&#x539F;&#x6765;&#x67D0;&#x4E9B;&#x6A21;&#x5F0F;&#x66FF;&#x6362;&#x6210;&#x65B0;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x5F97;&#x5230;&#x7684;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; String.replace(<span class="hljs-string">&quot;Hello&quot;</span>, <span class="hljs-string">&quot;e&quot;</span>, <span class="hljs-string">&quot;a&quot;</span>)
<span class="hljs-string">&quot;Hallo&quot;</span>
</code></pre>
<h3 id="duplicate2"><code>duplicate/2</code></h3>
<p>&#x8FD4;&#x56DE;&#x91CD;&#x590D;&#x4E86; n &#x904D;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; String.duplicate(<span class="hljs-string">&quot;Oh my &quot;</span>, <span class="hljs-number">3</span>)
<span class="hljs-string">&quot;Oh my Oh my Oh my &quot;</span>
</code></pre>
<h3 id="split2"><code>split/2</code></h3>
<p>&#x8FD4;&#x56DE;&#x628A;&#x539F;&#x5B57;&#x7B26;&#x4E32;&#x6309;&#x7167;&#x67D0;&#x4E2A;&#x6A21;&#x5F0F;&#x5206;&#x9694;&#x540E;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x5217;&#x8868;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; String.split(<span class="hljs-string">&quot;Hello World&quot;</span>, <span class="hljs-string">&quot; &quot;</span>)
[<span class="hljs-string">&quot;Hello&quot;</span>, <span class="hljs-string">&quot;World&quot;</span>]
</code></pre>
<h2 id="&#x7EC3;&#x4E60;">&#x7EC3;&#x4E60;</h2>
<p>&#x6211;&#x4EEC;&#x6765;&#x505A;&#x4E2A;&#x5C0F;&#x7684;&#x7EC3;&#x4E60;&#xFF0C;&#x8BC1;&#x660E;&#x5DF2;&#x7ECF;&#x638C;&#x63E1;&#x4E86;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x77E5;&#x8BC6;&#x3002;</p>
<h3 id="&#x91CD;&#x7EC4;&#x5B57;&#x7B26;&#x4E32;">&#x91CD;&#x7EC4;&#x5B57;&#x7B26;&#x4E32;</h3>
<p>A &#x548C; B &#x5982;&#x679C;&#x80FD;&#x901A;&#x8FC7;&#x91CD;&#x7EC4;&#x53D8;&#x5F97;&#x548C;&#x5BF9;&#x65B9;&#x4E00;&#x6837;&#xFF0C;&#x5C31;&#x8BA4;&#x4E3A;&#x4E24;&#x8005;&#x662F;&#x91CD;&#x7EC4;&#x5B57;&#x7B26;&#x4E32;&#x3002;&#x6BD4;&#x5982;&#xFF1A;</p>
<ul>
<li>A = super</li>
<li>B = perus</li>
</ul>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x91CD;&#x7EC4; A &#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x5143;&#x7D20;&#x5C31;&#x53EF;&#x4EE5;&#x5F97;&#x5230; B&#xFF0C;&#x53CD;&#x4E4B;&#x4EA6;&#x7136;&#x3002;</p>
<p>&#x90A3;&#x4E48;&#xFF0C;&#x600E;&#x4E48;&#x624D;&#x80FD;&#x7528; Elixir &#x5224;&#x65AD;&#x4E24;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x5426;&#x4E3A;&#x91CD;&#x7EC4;&#x5B57;&#x7B26;&#x4E32;&#x5462;&#xFF1F;&#x6700;&#x7B80;&#x5355;&#x7684;&#x529E;&#x6CD5;&#x5C31;&#x662F;&#x5BF9;&#x4E24;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x5B57;&#x7B26;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#xFF0C;&#x7136;&#x540E;&#x6BD4;&#x8F83;&#x6392;&#x5E8F;&#x540E;&#x7684;&#x7ED3;&#x679C;&#x662F;&#x5426;&#x76F8;&#x7B49;&#x3002;&#x6211;&#x4EEC;&#x6765;&#x8BD5;&#x8BD5;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Anagram</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">anagrams?</span></span>(a, b) <span class="hljs-keyword">when</span> is_binary(a) <span class="hljs-keyword">and</span> is_binary(b) <span class="hljs-keyword">do</span>
    sort_string(a) == sort_string(b)
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sort_string</span></span>(string) <span class="hljs-keyword">do</span>
    string
    |&gt; String.downcase()
    |&gt; String.graphemes()
    |&gt; Enum.sort()
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x5148;&#x770B;&#x4E00;&#x4E0B; <code>anagrams?/2</code>&#xFF0C;&#x9996;&#x5148;&#x6211;&#x4EEC;&#x4F1A;&#x68C0;&#x67E5;&#x63A5;&#x53D7;&#x7684;&#x53C2;&#x6570;&#x662F;&#x5426;&#x4E3A; binaries&#xFF0C;&#x8FD9;&#x662F; Elixir &#x68C0;&#x67E5;&#x53C2;&#x6570;&#x662F;&#x5426;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x65B9;&#x6CD5;&#x3002;
&#x7136;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x8C03;&#x7528;&#x628A;&#x5B57;&#x7B26;&#x4E32;&#x6309;&#x7167;&#x5B57;&#x6BCD;&#x8868;&#x6392;&#x5E8F;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x9996;&#x5148;&#x628A;&#x6240;&#x6709;&#x5B57;&#x7B26;&#x8F6C;&#x6362;&#x6210;&#x5C0F;&#x5199;&#xFF0C;&#x7136;&#x540E;&#x8C03;&#x7528; <code>String.graphemes</code> &#x5F97;&#x5230;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x5B57;&#x7D20;&#x7684;&#x5217;&#x8868;&#x3002;&#x601D;&#x8DEF;&#x5F88;&#x6E05;&#x6670;&#xFF0C;&#x5BF9;&#x5427;&#xFF1F;</p>
<p>&#x6211;&#x4EEC;&#x6765;&#x901A;&#x8FC7; iex &#x770B;&#x4E00;&#x4E0B;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; Anagram.anagrams?(&quot;Hello&quot;, &quot;ohell&quot;)
true

iex&gt; Anagram.anagrams?(&quot;Mar&#xED;a&quot;, &quot;&#xED;Mara&quot;)
true

iex&gt; Anagram.anagrams?(3, 5)
** (FunctionClauseError) no function clause matching in Anagram.anagrams?/2

    The following arguments were given to Anagram.anagrams?/2:

        # 1
        3

        # 2
        5

    iex:11: Anagram.anagrams?/2
</code></pre>
<p>&#x6B63;&#x5982;&#x4E0A;&#x9762;&#x5C55;&#x793A;&#x7684;&#xFF0C;&#x6700;&#x540E;&#x4E00;&#x6B21;&#x8C03;&#x7528; <code>anagrams?</code> &#x8FD4;&#x56DE;&#x4E86; <code>FunctionClauseError</code>&#xFF0C;&#x8FD9;&#x4E2A;&#x9519;&#x8BEF;&#x5C31;&#x662F;&#x544A;&#x8BC9;&#x6211;&#x4EEC;&#x6A21;&#x5757;&#x4E2D;&#x6CA1;&#x6709;&#x63A5;&#x53D7;&#x4E24;&#x4E2A;&#x975E;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x51FD;&#x6570;&#x3002;&#x8FD9;&#x6B63;&#x662F;&#x6211;&#x4EEC;&#x671F;&#x671B;&#x7684;&#x7ED3;&#x679C;&#xFF1A;&#x53EA;&#x63A5;&#x53D7;&#x5B57;&#x7B26;&#x4E32;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#xFF0C;&#x5176;&#x4ED6;&#x90FD;&#x4E0D;&#x5141;&#x8BB8;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="comprehensions.html" class="navigation navigation-prev " aria-label="Previous page: 推导">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="date-time.html" class="navigation navigation-next " aria-label="Next page: 日期和时间">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.2.0","title":"字符串","level":"1.15","depth":1,"next":{"title":"日期和时间","level":"1.16","depth":1,"path":"lessons/basics/date-time.md","ref":"lessons/basics/date-time.md","articles":[]},"previous":{"title":"推导","level":"1.14","depth":1,"path":"lessons/basics/comprehensions.md","ref":"lessons/basics/comprehensions.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/basics/strings.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

